package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.bean.MarketRole;
import com.sixth.bean.MarketRoleExample;
import com.sixth.bean.bo.RoleListBO;
import com.sixth.bean.bo.RolePermissionBO;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.vo.RoleOptionVO;
import com.sixth.bean.vo.RolePermissionVO;
import com.sixth.bean.vo.SystemPermissions;
import com.sixth.mapper.MarketPermissionDetailMapper;
import com.sixth.mapper.MarketPermissionModuleMapper;
import com.sixth.mapper.MarketRoleMapper;
import com.sixth.mapper.RolePermissionMapper;
import com.sixth.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements AdminRoleService {

    @Autowired
    MarketPermissionModuleMapper marketPermissionModuleMapper;
    @Autowired
    MarketPermissionDetailMapper marketPermissionDetailMapper;
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Override

    public BasePageInfoVo options() {
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(null);

        List<RoleOptionVO> list = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            RoleOptionVO roleOptionVO = new RoleOptionVO();
            roleOptionVO.setLabel(marketRole.getName());
            roleOptionVO.setValue(marketRole.getId());
            list.add(roleOptionVO);
        }

        PageInfo<MarketRole> PageInfo = new PageInfo<>(marketRoles);

        BasePageInfoVo<RoleOptionVO> PageInfoVo = new BasePageInfoVo<>();
        PageInfoVo.setLimit(PageInfo.getPageSize());
        PageInfoVo.setTotal(PageInfo.getTotal());
        PageInfoVo.setPage(1);
        PageInfoVo.setPages(1);
        PageInfoVo.setList(list);

        return PageInfoVo;
    }

    @Override
    public BasePageInfoVo list(RoleListBO roleListBO) {
        PageHelper.startPage(roleListBO.getPage(), roleListBO.getLimit());

        MarketRoleExample marketRoleExample = new MarketRoleExample();
        marketRoleExample.setOrderByClause(roleListBO.getSort() + " " + roleListBO.getOrder());
        if (roleListBO.getName() != null){
            MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
            criteria.andNameLike("%"+roleListBO.getName()+"%");
        }

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);

        if(marketRoles == null){
            return null;
        }

        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);
        BasePageInfoVo<MarketRole> roleListVOBasePageInfoVo = new BasePageInfoVo<>();
        roleListVOBasePageInfoVo.setPage(marketRolePageInfo.getPageNum());
        roleListVOBasePageInfoVo.setPages(marketRolePageInfo.getPages());
        roleListVOBasePageInfoVo.setLimit(marketRolePageInfo.getPageSize());
        roleListVOBasePageInfoVo.setTotal(marketRolePageInfo.getTotal());
        roleListVOBasePageInfoVo.setList(marketRoles);

        return roleListVOBasePageInfoVo;
    }

    @Override
    public RolePermissionVO permissions(Integer roleID) {

        //先查询所有权限一二三级表
        List<SystemPermissions> systemPermissions
                = marketPermissionModuleMapper.selectAllByType();

        //根据roleId 查询对应权限;
        List<String> assignedPermissions
                = rolePermissionMapper.selectByRoleId(roleID);
        if ("*".equals(assignedPermissions.get(0))) {
            assignedPermissions = marketPermissionDetailMapper.selectAllPermission();
        }

        RolePermissionVO rolePermissionVO = new RolePermissionVO();
        rolePermissionVO.setAssignedPermissions(assignedPermissions);
        rolePermissionVO.setSystemPermissions(systemPermissions);

        return rolePermissionVO;
    }

    @Transactional
    @Override
    public int permissions(RolePermissionBO rolePermissionBO) {
        //先删除再添加

        int code = rolePermissionMapper.updateDeletedByRoleId(rolePermissionBO.getRoleId());
        if (code == 0) {
            return 404;
        }
        rolePermissionBO.setAddTime(new Date());
        code = rolePermissionMapper.insertAddNewAssociation(rolePermissionBO);
        if(code == 0){
            return 404;
        }

        return 200;
    }

    @Override
    public List<String> getRolesName(Integer[] roleIds) {

        List<String> rolesName = marketRoleMapper.selectRoleNameByRoleIds(roleIds);

        return rolesName;
    }

    @Override
    public List<String> getPermissionApi(Integer[] roleIds) {

        for (Integer roleId : roleIds) {
            if(roleId == 1){
                ArrayList<String> permissions = new ArrayList<>();
                permissions.add("*");
                return permissions;
            }
        }

        return marketRoleMapper.selectPermissionsApiByRole(roleIds);


    }


}
